package com.lanou3g.k12api.utils;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class RandomEntityUtils {
    private static final String INTEGER_TYPE = "class java.lang.Integer";
    private static final String LONG_TYPE = "class java.lang.Long";
    private static final String DOUBLE_TYPE = "class java.lang.Double";
    private static final String STRING_TYPE = "class java.lang.String";
    private static final String DATE_TYPE = "class java.util.Date";

    public static <T> List<T> random(Class<T> cls, int size) {
        List<T> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            list.add(random(cls));
        }
        return list;
    }

    public static <T> T random(Class<T> cls) {
        T t = null;
        try {
            t = cls.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Field[] fields = cls.getDeclaredFields();
        Random r = new Random();
        for (Field field : fields) {
            field.setAccessible(true);
            String fieldName = field.getName();
            try {
                if ("status".equals(fieldName)) {
                    field.set(t, r.nextDouble() >= 0.1 ? 0 : 1);
                } else if ("image".equals(fieldName) || "cover".equals(fieldName)) {
                    field.set(t, "https://wx3.sinaimg.cn/large/007dOZvply1g2curtd7flj31hc0mz15p.jpg");
                } else if ("sort".equals(fieldName)) {
                    field.set(t, r.nextInt(10));
                } else if ("link".equals(fieldName)) {
                    field.set(t, "/course/" + (r.nextInt(1000) + 1));
                } else if ("level".equals(fieldName)) {
                    field.set(t, r.nextInt(6));
                } else if ("price".equals(fieldName)) {
                    field.set(t, (r.nextInt(200) + 1) * 10);
                } else {
                    String type = field.getGenericType().toString();
                    switch (type) {
                        case INTEGER_TYPE:
                            field.set(t, r.nextInt(100));
                            break;
                        case LONG_TYPE:
                            field.set(t, Math.abs(r.nextLong()));
                            break;
                        case DOUBLE_TYPE:
                            field.set(t, r.nextDouble() * 100);
                            break;
                        case STRING_TYPE:
                            field.set(t, randomCNString(20));
                            break;
                        case DATE_TYPE:
                            field.set(t, randomDate("2018-01-01", "2019-04-23"));
                            break;
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return t;
    }

    public static String randomCNString(int maxLength) {
        return randomCNString(0, maxLength);
    }

    public static String randomCNString(int minLength, int maxLength) {
        //随机生成常见汉字
        StringBuilder str = new StringBuilder();
        int highCode;
        int lowCode;
        Random random = new Random();
        int length = random.nextInt(maxLength) + minLength + 1;
        for (int i = 0; i < length; i++) {
            String c = "";
            highCode = (176 + Math.abs(random.nextInt(39))); //B0 + 0~39(16~55) 一级汉字所占区
            lowCode = (161 + Math.abs(random.nextInt(93))); //A1 + 0~93 每区有94个汉字

            byte[] b = new byte[2];
            b[0] = (Integer.valueOf(highCode)).byteValue();
            b[1] = (Integer.valueOf(lowCode)).byteValue();

            try {
                c = new String(b, "GBK");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            str.append(c);
        }
        return str.toString();
    }

    /**
     * 获取随机日期
     *
     * @param beginDate 起始日期，格式为：yyyy-MM-dd
     * @param endDate   结束日期，格式为：yyyy-MM-dd
     * @return
     */
    private static Date randomDate(String beginDate, String endDate) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date start = format.parse(beginDate);// 构造开始日期
            Date end = format.parse(endDate);// 构造结束日期
            // getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
            if (start.getTime() >= end.getTime()) {
                return null;
            }
            long date = random(start.getTime(), end.getTime());

            return new Date(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static long random(long begin, long end) {
        long rtn = begin + (long) (Math.random() * (end - begin));
        // 如果返回的是开始时间和结束时间，则递归调用本函数查找随机值
        if (rtn == begin || rtn == end) {
            return random(begin, end);
        }
        return rtn;
    }

}
